#include "StdAfx.h"
#include "UnMergeDeviceCmd.h"

RegisterCommandClass<CUnMergeDeviceCmd> RegisterUnMergeDeviceCmd(ClassNameToString(CUnMergeDeviceCmd));

CUnMergeDeviceCmd::CUnMergeDeviceCmd(void)
{
	m_pData = NULL;
}


CUnMergeDeviceCmd::~CUnMergeDeviceCmd(void)
{
	delete m_pData;
}


bool CUnMergeDeviceCmd::Execute()
{
	if (!CMyBaseCommand::Execute())
		return false;

	if (!m_pData)
	{
		if (!GetDataFromXML())
			return false;
	}

	long lLayerId = GetActiveLayerId();
	list<CSystemDisplayObj*> lstSysDisForUnPack;
	list<CSubSystem*> lstSubsys = m_pData->m_lstSysForUnPack;
	for (list<CSubSystem*>::iterator it=lstSubsys.begin(); it!=lstSubsys.end(); it++)
	{
		CDisplayObj* pDisobj = (*it)->GetDisplayObjByLayerID(lLayerId);
		if (pDisobj && pDisobj->IsSystemDisplayObj())
		{
			lstSysDisForUnPack.push_back((CSystemDisplayObj*)pDisobj);
		}
	}

	return CMergeDeviceCmd::UnPackSubsys(lstSysDisForUnPack);
}

bool CUnMergeDeviceCmd::Unexecute()
{
	if (!CMyBaseCommand::Unexecute())
		return false;

	GetHoopsView()->GetSelection()->DeSelectAll();

	if (!m_pData)
	{
		if (!GetDataFromXML())
			return false;
	}

	long lLayerId = GetActiveLayerId();
	list<CSystemDisplayObj*> lstSysDisForPack;
	list<CSubSystem*> lstSubsys = m_pData->m_lstSysForUnPack;
	for (list<CSubSystem*>::iterator it=lstSubsys.begin(); it!=lstSubsys.end(); it++)
	{
		CDisplayObj* pDisobj = (*it)->GetDisplayObjByLayerID(lLayerId);
		if (pDisobj && pDisobj->IsSystemDisplayObj())
		{
			lstSysDisForPack.push_back((CSystemDisplayObj*)pDisobj);
		}
	}

	return CMergeDeviceCmd::PackSubsys(lstSysDisForPack);
}

bool CUnMergeDeviceCmd::SaveDataToXML()
{
	Begin_SaveDataToXML
	{
		TiXmlElement itemUnmergeSubsysData("Unmerge_subsys_data");
		list<CSubSystem*> lstSubsys = m_pData->m_lstSysForUnPack;
		for (list<CSubSystem*>::iterator it=lstSubsys.begin(); it!=lstSubsys.end(); it++)
		{
			CSubSystem* pSubsys = *it;
			TiXmlElement itemWire("Sub_system");
			itemWire.SetAttribute("Id", pSubsys->GetId());
			itemUnmergeSubsysData.InsertEndChild(itemWire);
		}
		itemCmd.InsertEndChild(itemUnmergeSubsysData);
	}
	End_SaveDataToXML

	return true;
}

bool CUnMergeDeviceCmd::GetDataFromXML()
{
	if (m_pData)
		return true;
	else
		m_pData = new UnMergeDeviceData;

	CSystem* pSystem = CModelObjOption::GetIModelObjEventHandler()->GetActiveSystem();
	Begin_GetDataFromXML
	{
		list<CSubSystem*> lstSubsysForUnmerge;
		TiXmlElement* elemMergeCblData = elemCmd->FirstChildElement("Unmerge_subsys_data");
		if (elemMergeCblData)
		{
			for (TiXmlElement* itemSubsys=elemMergeCblData->FirstChildElement("Sub_system");
				itemSubsys; itemSubsys=itemSubsys->NextSiblingElement("Sub_system"))
			{
				int nId = 0;
				itemSubsys->Attribute("Id", &nId);
				CSubSystem* pSubsys = (CSubSystem*)pSystem->GetEntObjByID(nId);
				if (!pSubsys || !pSubsys->IsSubSystem())
					return false;

				lstSubsysForUnmerge.push_back(pSubsys);
			}
		}
		m_pData->m_lstSysForUnPack = lstSubsysForUnmerge;
	}
	End_GetDataFromXML

	return true;
}

bool CUnMergeDeviceCmd::DeleteData()
{
	delete m_pData;
	m_pData = NULL;
	return true;
}

bool CUnMergeDeviceCmd::PrepareData(list<CSystemDisplayObj *> &lstSysDis)
{
	list<CSubSystem*> lstSysEnt;
	for (list<CSystemDisplayObj*>::iterator it=lstSysDis.begin(); it!=lstSysDis.end(); it++)
	{
		CEntityObj* pEnt = (*it)->GetEntityObj();
		if (pEnt && pEnt->IsSubSystem())
		{
			lstSysEnt.push_back((CSubSystem*)pEnt);
		}
	}

	m_pData = new UnMergeDeviceData;
	m_pData->m_lstSysForUnPack = lstSysEnt;

	End_PrepareData;
	return true;
}

